package com.amigo.online.provider.user.util;

import java.util.Arrays;
import java.util.List;

/**
 * @author kml 709628161@qq.com
 * @ClassName: StandardizePhoneUtil
 * @Description: 规范巴西手机号码格式
 * @date 2019年3月14日 上午10:45:19
 */
public class StandardizePhoneUtil {

    /**
     * 巴西手机号码问题：有可能首位是0。正常时10位，如果是11位，则第三位是9。012945678912,01245678912,12945678912,1245678912都是同一个手机号码
     */
    public static List<String> standard(String phone) {
        if (phone == null) {
            return null;
        }
        // 有0有9 012945678912
        String with0with9 = "";

        // 有0无9 01245678912
        String with0without9 = "";

        // 无0有9 12945678912
        String without0with9 = "";

        // 无0无9 1245678912
        String without0without9 = "";
        if (phone.charAt(0) == '0') {
            //有0有9
            if (phone.charAt(3) == '9') {
                with0with9 = phone;
                with0without9 = removeCharAt(phone, 3);
                without0with9 = removeCharAt(phone, 0);
                without0without9 = removeCharAt(removeCharAt(phone, 0), 2);
            } else {//有0无9
                with0with9 = new StringBuffer(phone).insert(3, '9').toString();
                with0without9 = phone;
                without0with9 = new StringBuffer(removeCharAt(phone, 0)).insert(2, '9').toString();
                without0without9 = removeCharAt(phone, 0);
            }
        } else {
            //无0有9
            if (phone.charAt(2) == '9') {
                with0with9 = new StringBuffer(phone).insert(0, '0').toString();
                with0without9 = removeCharAt(new StringBuffer(phone).insert(0, '0').toString(), 3);
                without0with9 = phone;
                without0without9 = removeCharAt(phone, 2);
            } else {//无0无9
                with0with9 = new StringBuffer(phone).insert(0, '0').insert(3, '9').toString();
                with0without9 = new StringBuffer(phone).insert(0, '0').toString();
                without0with9 = new StringBuffer(phone).insert(2, '9').toString();
                without0without9 = phone;
            }
        }
        return Arrays.asList(with0with9, with0without9, without0with9, without0without9);
    }

    /**
     * @param @param  str
     * @param @param  index
     * @param @return
     * @return String
     * @throws
     * @Title: removeCharAt
     * @Description: eg : "welcome".removeCharAt(2) ="wecome"
     */
    private static String removeCharAt(String str, int index) {
        String strPrefix = str.substring(0, index);
        String strSuffix = str.substring(index + 1);
        return strPrefix + strSuffix;
    }


    public static void main(String[] args) {
        System.out.println(standard("31975015680"));

    }
}
